package service.impl;

import dao.PoMainMapper;
import dao.ProductMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import pojo.*;
import service.PoMainService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Admin
 * @date 2022/11/18 18:29
 * @Description TODO
 * @Version 1.0
 **/
@Service
public class PoMainServiceImpl implements PoMainService {
    @Resource
    private PoMainMapper poMainMapper;
    @Resource
    private ProductMapper productMapper;

    @Override
    public int getCount(PoMain poMain) {
        return poMainMapper.getCount(poMain);
    }

    @Override
    public List<PoMain> getAllByPage(Page<PoMain> p, PoMain poMain) {
        return poMainMapper.getAllByPage(p, poMain);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void add(PoMain poMain) {
        if (poMainMapper.checkPomainByPoID(String.valueOf(poMain.getPoId())) != null) {
            //删除
            del(poMain);
            //新增
            add1(poMain);
        } else {
            add1(poMain);
        }

    }

    public void add1(PoMain poMain) {
        poMain.setPoTotal(poMain.getTipFee() + poMain.getProductTotal());
        poMainMapper.AddPoMain(poMain);
        List<PoItem> poList = poMain.getPoItems();
        for (PoItem poItem : poList) {
            PoItem p = productMapper.getPoNum(poItem);
            poItem.setPoId(poMain.getPoId());
            poItem.setNum(poItem.getNewNum());
            poItem.setItemPrice(poItem.getNum() * poItem.getUnitPrice());
            poItem.setPoNum(poItem.getPoNum());
            poMainMapper.AddPoItem(poItem);
            productMapper.updatePoNumAdd(poItem.getProductCode(), poItem.getNewNum());
        }
    }

    @Override
    public void del(PoMain poMain) {
        PoItem poItem = new PoItem();
        poItem.setPoId(poMain.getPoId());
        List<PoItem> poList = poMainMapper.cxPoMainByPoID(poMain);
        for (PoItem poItem1 : poList) {
            poItem.setProductCode(poItem1.getProductCode());
            PoItem p = productMapper.getPoNum(poItem);
            poItem.setPoNum(p.getPoNum());
            poItem.setNewNum(poItem1.getNum());
            poMainMapper.DelPoItem(poItem);
            productMapper.updatePoNumDel(poItem.getProductCode(), poItem.getNewNum());
        }
        poMainMapper.DelPoMain(poMain);
    }

    @Override
    public PoMain updatePomainByPoID(BigDecimal pd) {
        return poMainMapper.updatePomainByPoID(pd);
    }

    //了结
    @Override
    public Page<PoMain> getPoMainPage(Page<PoMain> page, PoMain poMain, int payType) {
        Page<PoMain> PoMainpage = new Page<>();
        //设置当前页
        PoMainpage.setCurrentPage(page.getCurrentPage());
        //设置每页条数
        PoMainpage.setPageSize(page.getPageSize());
        //设置每页数据
        PoMainpage.setLists(poMainMapper.getPoMainPage(page, poMain, payType));
        //设置总条数
        PoMainpage.setTotalRecord(poMainMapper.getCountPoMain(poMain, payType));
        return PoMainpage;
    }

    //入库
    @Override
    public Page<PoMain> inboundPage(Page<PoMain> page, PoMain poMain, int payType) {
        Page<PoMain> inboundPage = new Page<>();
        //设置当前页
        inboundPage.setCurrentPage(page.getCurrentPage());
        //设置每页条数
        inboundPage.setPageSize(page.getPageSize());
        //设置每页数据
        inboundPage.setLists(poMainMapper.inboundAllByPage(page, poMain, payType));
        //设置总条数
        inboundPage.setTotalRecord(poMainMapper.inboundCountPoMain(poMain, payType));
        return inboundPage;
    }

    @Override
    public int updInbound(PoMain poMain) {
        int i = poMainMapper.updateInbound(poMain);
        List<PoItem> list = poMainMapper.getNum(poMain.getPoId());
        for (PoItem p : list) {
            Product product = new Product();
            Stockrecord stockrecord = new Stockrecord();
            stockrecord.setProductCode(p.getProductCode());
            stockrecord.setOrderCode(String.valueOf(p.getPoId()));
            stockrecord.setCreateUser(poMain.getStockUser());
            stockrecord.setStockNum(p.getNum());
            poMainMapper.addStock(stockrecord);
            product.setProductCode(stockrecord.getProductCode());
            poMainMapper.updNum(productMapper.getProduct(product), p.getNum());
        }
        return i;
    }

    @Override
    public Page<PoMain> outMoneyPage(Page<PoMain> page, PoMain poMain, int payType) {
        Page<PoMain> outMoneyPage = new Page<>();
        //设置当前页
        outMoneyPage.setCurrentPage(page.getCurrentPage());
        //设置每页条数
        outMoneyPage.setPageSize(page.getPageSize());
        //设置每页数据
        outMoneyPage.setLists(poMainMapper.outMoneyAllByPage(page, poMain, payType));
        //设置总条数
        outMoneyPage.setTotalRecord(poMainMapper.outMoneyCountPoMain(poMain, payType));
        return outMoneyPage;
    }

    @Override
    public int updateoutMoney(PoMain poMain) {
        return poMainMapper.updateoutMoney(poMain);
    }

    @Override
    public int updateoutMoney1(PoMain poMain) {
        return poMainMapper.updateoutMoney1(poMain);
    }

    @Override
    public int addoutMoney(Payrecord payrecord) {
        return poMainMapper.addoutMoney(payrecord);
    }

    @Override
    public List<PoMain> poReportPage(Page<PoMain> page, PoMain poMain) {
        return poMainMapper.poReportPage(page,poMain);
    }

    @Override
    public int poReportCount(Page<PoMain> page, PoMain poMain) {
        return poMainMapper.poReportCount(page,poMain);
    }

    @Override
    public List<PoMain> getPoAll() {
        PoMain poMain=new PoMain();
//        poMain.setPoMainCount(poMainMapper.getPoCount());
//        poMain.setEndCount(poMainMapper.getEndCountInt());
//        poMain.setPayPrice(poMainMapper.getPayPriceFloat());
//        poMain.setPoTotalPrice(poMainMapper.getPoTotalPrice());
        List<PoMain> poMainList = new ArrayList<>();
        poMainList.add(poMain);
        System.out.println(poMain);
        return poMainList;
    }

    @Override
    public List<PoMain> poReportPage1(PoMain pomain) {
        return poMainMapper.poReportPage1(pomain);
    }

    @Override
    public int poReportCount1(PoMain pomain) {
        return poMainMapper.poReportCount1(pomain);
    }

    @Override
    public int addoutMoney1(Payrecord payRecord) {
        return poMainMapper.addoutMoney1(payRecord);
    }

    @Override
    public void endUpd(PoMain poMain) {
        poMainMapper.endUpdate(poMain);
    }

    @Override
    public int getPomainCount(Page<PoMain> page, PoMain poMain) {
        return poMainMapper.PoMainCount(page, poMain);
    }

    @Override
    public List<PoMain> pomainAllByPage(Page<PoMain> page, PoMain poMain) {
        return poMainMapper.poMainAllByPage(page, poMain);
    }


    @Override
    public void update(PoMain poMain) {
        poMainMapper.update(poMain);
    }

    @Override
    public List<PoMain> getAll() {
        return null;
    }

    @Override
    public List<PoMain> getAll1() {
        return null;
    }


    @Override
    public PoMain getPoMainByPoID(String poID) {
        return poMainMapper.getPoMainByPoID(poID);
    }

    @Override
    public List<PoItem> getItemsByPoID(String poID) {
        return poMainMapper.getItemsByPoID(poID);
    }

    @Override
    public PoMain check(String poId) {
        PoMain poMain = poMainMapper.getPoMainByPoID(poId);
        return poMain;
    }

}


